package net.comic.controllers

import net.comic.entities.Author

class AuthorController {

    def index = { redirect(action: "list", params: params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def list = {
        params.max = Math.min(params.max ? params.max.toInteger() : 10,  100)
        [authorInstanceList: Author.list(params), authorInstanceTotal: Author.count()]
    }

    def create = {
        def authorInstance = new Author()
        authorInstance.properties = params
        return [authorInstance: authorInstance]
    }

    def save = {
        def authorInstance = new Author(params)
        if (!authorInstance.hasErrors() && authorInstance.save()) {
            flash.message = "author.created"
            flash.args = [authorInstance.id]
            flash.defaultMessage = "Author ${authorInstance.id} created"
            redirect(action: "show", id: authorInstance.id)
        }
        else {
            render(view: "create", model: [authorInstance: authorInstance])
        }
    }

    def show = {
        def authorInstance = Author.get(params.id)
        if (!authorInstance) {
            flash.message = "author.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Author not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [authorInstance: authorInstance]
        }
    }

    def edit = {
        def authorInstance = Author.get(params.id)
        if (!authorInstance) {
            flash.message = "author.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Author not found with id ${params.id}"
            redirect(action: "list")
        }
        else {
            return [authorInstance: authorInstance]
        }
    }

    def update = {
        def authorInstance = Author.get(params.id)
        if (authorInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (authorInstance.version > version) {
                    
                    authorInstance.errors.rejectValue("version", "author.optimistic.locking.failure", "Another user has updated this Author while you were editing")
                    render(view: "edit", model: [authorInstance: authorInstance])
                    return
                }
            }
            authorInstance.properties = params
            if (!authorInstance.hasErrors() && authorInstance.save()) {
                flash.message = "author.updated"
                flash.args = [params.id]
                flash.defaultMessage = "Author ${params.id} updated"
                redirect(action: "show", id: authorInstance.id)
            }
            else {
                render(view: "edit", model: [authorInstance: authorInstance])
            }
        }
        else {
            flash.message = "author.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Author not found with id ${params.id}"
            redirect(action: "edit", id: params.id)
        }
    }

    def delete = {
        def authorInstance = Author.get(params.id)
        if (authorInstance) {
            try {
                authorInstance.delete()
                flash.message = "author.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Author ${params.id} deleted"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "author.not.deleted"
                flash.args = [params.id]
                flash.defaultMessage = "Author ${params.id} could not be deleted"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "author.not.found"
            flash.args = [params.id]
            flash.defaultMessage = "Author not found with id ${params.id}"
            redirect(action: "list")
        }
    }
}
